//
//  OrderDetailCell.m
//  JiongTu
//
//  Created by 杨璟 on 16/3/29.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "OrderDetailCell.h"

#import "JTDriverTrip.h"
#import "JTPassengerTrip.h"
#import "UIView+Extension.h"
#import "JTUser.h"

#import <AVOSCloud.h>
#import <Masonry.h>
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"

#import "PassengerView.h"

@interface OrderDetailCell ()
/**
 *  关闭键
 */
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
/**
 *  用户头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *userIcon;
/**
 *  背景头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *bgIcon;
/**
 *  起点城区标签
 */
@property (weak, nonatomic) IBOutlet UILabel *startLabel;
/**
 *  终点城区标签
 */
@property (weak, nonatomic) IBOutlet UILabel *endLabel;
/**
 *  价格标签
 */
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
/**
 *  终点街道标签
 */
@property (weak, nonatomic) IBOutlet UILabel *destinationLabel;
/**
 *  用户名称标签
 */
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
/**
 *  出行次数标签
 */
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
/**
 *  行程备注标签
 */
@property (weak, nonatomic) IBOutlet UILabel *remarkLabel;
/**
 *  车牌标签
 */
@property (weak, nonatomic) IBOutlet UILabel *carPlateLabel;
/**
 *  车型标签
 */
@property (weak, nonatomic) IBOutlet UILabel *carTypeLabel;
/**
 *  『同址再找』按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *sameFind;
/**
 *  『支付』按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *alipay;
/**
 *  『删除订单』按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *deleteOrder;
/**
 *  『已到达』按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *arrivedBtn;

/**
 *  所有乘客头像
 */
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *passengerBtns;
/**
 *  所有乘客名称
 */
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *passengerLabel;
/**
 *  头像加名称整合view
 */
@property (weak, nonatomic) IBOutlet UIStackView *passengerViews;

@property (nonatomic, strong) NSMutableArray *passengersOfDriver;

@end

@implementation OrderDetailCell

- (NSMutableArray *)passengersOfDriver {
    if (!_passengersOfDriver) {
        _passengersOfDriver = [NSMutableArray array];
    }
    return _passengersOfDriver;
}

- (void)setSameFind:(UIButton *)sameFind {
    _sameFind = sameFind;
    
    [sameFind addTarget:self action:@selector(findSame) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setDeleteOrder:(UIButton *)deleteOrder {
    _deleteOrder = deleteOrder;
    
    [deleteOrder addTarget:self action:@selector(deleteTrip) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setTrip:(id)trip {
    _trip = trip;
    if ([trip isKindOfClass:[JTPassengerTrip class]]) {
        JTPassengerTrip *passengerTrip = trip;
        [self setViewWithPassengerTrip:passengerTrip];
    } else if ([trip isKindOfClass:[JTDriverTrip class]]) {
        JTDriverTrip *driverTrip = trip;
        [self setViewWithDriverTrip:driverTrip];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //固定为storyboard中宽度的一半
    self.userIcon.layer.cornerRadius = 28;
    self.userIcon.layer.masksToBounds = YES;
    self.userIcon.layer.borderWidth = 1;
    self.userIcon.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    for (UIButton *btn in self.passengerBtns) {
        btn.layer.cornerRadius = btn.width * 0.5;
        btn.layer.masksToBounds = YES;
    }
    
}

#pragma mark - 设置页面

/**
 *  设置一个乘客行程
 */
- (void)setViewWithPassengerTrip:(JTPassengerTrip *)passengerTrip {
    //起止地设置
    self.startLabel.text = passengerTrip.startLocation;
    self.endLabel.text = passengerTrip.endLocation;
    self.destinationLabel.text = passengerTrip.endStreet;
    
    //根据备注内容设置
    if ([passengerTrip.remark isEqualToString:@""]) {
        self.remarkLabel.text = @"无";
    } else {
        self.remarkLabel.text = passengerTrip.remark;
    }
    
    //根据订单状态设置功能按钮
    if ([passengerTrip.isOrdered isEqual:@(0)]) {
        self.alipay.hidden = YES;
        self.sameFind.hidden = NO;
    } else if ([passengerTrip.isOrdered isEqual:@(1)]) {
        self.alipay.hidden = NO;
        self.sameFind.hidden = YES;
    } else if ([passengerTrip.isOrdered isEqual:@(2)]) {
        self.alipay.hidden = YES;
        self.sameFind.hidden = YES;
    }
    
    //根据司机是否存在设置司机信息
    if (passengerTrip.driver.nickname) {
        self.nameLabel.text = passengerTrip.driver.nickname;
        self.countLabel.text = [passengerTrip.driver.current stringValue];
        self.carPlateLabel.text = passengerTrip.driver.carNumber;
        self.carTypeLabel.text = passengerTrip.driver.carName;
    } else {
        self.nameLabel.text = @"暂无";
        self.countLabel.text = @"暂无";
        self.carPlateLabel.text = @"暂无";
        self.carTypeLabel.text = @"暂无";
    }
    
    [self.userIcon sd_setImageWithURL:passengerTrip.driver.iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    [self.bgIcon sd_setImageWithURL:passengerTrip.driver.iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    
    //乘客订单中不包含的信息和功能
    self.priceLabel.hidden = YES;
    self.passengerViews.hidden = YES;
    self.arrivedBtn.hidden = YES;
    
}
/**
 *  设置一个司机行程
 */
- (void)setViewWithDriverTrip:(JTDriverTrip *)driverTrip {
    //司机行程不应有的按钮
    self.alipay.hidden = YES;
    
    //设置司机行程应有的信息
    self.startLabel.text = driverTrip.startLocation;
    self.endLabel.text = driverTrip.endLocation;
    self.nameLabel.text = driverTrip.driver.nickname;
    self.countLabel.text = [driverTrip.driver.current stringValue];
    self.destinationLabel.text = driverTrip.endStreet;
    self.carTypeLabel.text = driverTrip.driver.carName;
    self.carPlateLabel.text = driverTrip.driver.carNumber;
    self.priceLabel.hidden = NO;
    self.priceLabel.text = [NSString stringWithFormat:@"（%@元/位）", driverTrip.price];
    
    if ([driverTrip.remark isEqualToString:@""]) {
        self.remarkLabel.text = @"无";
    } else {
        self.remarkLabel.text = driverTrip.remark;
    }
    //根据乘客数量判断是否显示同址再找或已到达按钮
    if (driverTrip.passenger.count) {
        self.sameFind.hidden = YES;
        self.arrivedBtn.hidden = NO;
    } else {
        self.sameFind.hidden = NO;
        self.arrivedBtn.hidden = YES;
    }

//    for (JTPassengerTrip *passenger in driverTrip.passenger) {
//        UIButton *btn = self.passengerBtns[[driverTrip.passenger indexOfObject:passenger]];
//        UILabel *label = self.passengerLabel[[driverTrip.passenger indexOfObject:passenger]];
//        label.text = passenger.passenger.nickname;
//        [btn sd_setBackgroundImageWithURL:passenger.passenger.iconPath forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"头像占位"]];
//    }
    
    if (driverTrip.passenger.count) {
        self.passengerViews.hidden = NO;
        self.passengersOfDriver = nil;
        for (JTPassengerTrip *passenger in driverTrip.passenger) {
            for (NSInteger i = 0; i < [passenger.orderSeatNumber integerValue]; i++) {
                [self.passengersOfDriver addObject:passenger];
            }
        }
        for (NSInteger i = 0; i < self.passengersOfDriver.count; i++) {
            JTPassengerTrip *passenger = self.passengersOfDriver[i];
            UIButton *btn = self.passengerBtns[i];
            UILabel *label = self.passengerLabel[i];
            label.text = passenger.passenger.nickname;
            [btn sd_setBackgroundImageWithURL:passenger.passenger.iconPath forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"头像占位"]];
        }
    } else {
        self.passengerViews.hidden = YES;
    }
    
//    for (UIButton *btn in self.passengerBtns) {
//        NSUInteger position = [self.passengerBtns indexOfObject:btn];
//        NSUInteger maxPosi = driverTrip.passenger.count;
//        if ((position <= maxPosi - 1) && maxPosi) {
//            JTPassengerTrip *passenger = driverTrip.passenger[position];
//            [btn sd_setBackgroundImageWithURL:passenger.passenger.iconPath forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"头像占位"]];
//            UILabel *label = self.passengerLabel[position];
//            label.text = passenger.passenger.nickname;
//        } else {
//            UILabel *label = self.passengerLabel[position];
//            label.text = @"";
//            [btn setBackgroundImage:[UIImage imageNamed:@"车类详情页_16"] forState:UIControlStateNormal];
//        }
//    }
    
    
    [self.userIcon sd_setImageWithURL:driverTrip.driver.iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    [self.bgIcon sd_setImageWithURL:driverTrip.driver.iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    
}

#pragma mark - 点击事件

- (IBAction)closeClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(closeButtonDidClick:)]) {
        [self.delegate closeButtonDidClick:self.indexPath];
    }
}

- (void)findSame{
    if ([self.delegate respondsToSelector:@selector(findSamePassengerTrip:)]) {
        [self.delegate findSamePassengerTrip:self.trip];
    }
}

- (void)deleteTrip{
    if ([self.delegate respondsToSelector:@selector(deletePassengerTrip:)]) {
        [self.delegate deletePassengerTrip:self.trip];
    }
}

- (IBAction)arrivedBtnClick:(UIButton *)sender {
    sender.hidden = YES;
    if ([self.delegate respondsToSelector:@selector(catchedBtnClick:)]) {
        [self.delegate catchedBtnClick:self.trip];
    }
}

- (IBAction)passIconClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(passengerIconClick:withDriverTrip:)]) {
        NSUInteger position = [self.passengerBtns indexOfObject:sender];
        JTDriverTrip *driverTrip = self.trip;
        if (position < self.passengersOfDriver.count) {
            [self.delegate passengerIconClick:self.passengersOfDriver[position] withDriverTrip:driverTrip];
        }
    }
}

- (IBAction)alipayClick:(id)sender {
    JTPassengerTrip *toPayTrip = self.trip;
    
    [[NSUserDefaults standardUserDefaults]setObject:@(0) forKey:@"pay"];
    
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"pay"]);
    
    if ([self.delegate respondsToSelector:@selector(payForTrip:)]) {
        [self.delegate payForTrip:toPayTrip];
    }
}

@end
